<?php
ini_set('error_reporting', 6143);
ini_set('display_errors','2');
define('IN_LC', true);
define('MM_ID', 3);

include_once("validate.php");
include_once("includes/style.php");
include_once("includes/driver_details.php");
include_once("includes/addMessage.php");


$done = false;
// are we showing an actual profile?
if(isset($_GET['id']))
{
	// Create query
	$q = 'SELECT *, pf_drivername as user_drivername FROM ' . 
					USERS_TABLE . ' u, '. USERSGROUPS_TABLE . ' g, ' . DRIVERNAME_TABLE . ' d 
				 	WHERE g.user_id = u.user_id AND g.group_id = 3 AND u.user_id = d.user_id AND u.user_id = ' . $_GET['id'];
			
	
	// Run query
	$res = $db->sql_query($q);
	if ($row = $db->sql_fetchrow($res))
	{
		$style = new style(true);	
		include_once('includes/side_menu.php');
		$style->addRenderLeft(new side_menu('users', $_GET['id'], $row));
		if(isset($_GET['mode']))
		{
			$driver = new driver_details($row, $_GET['mode']);
		}
		else
		{
			$driver = new driver_details($row, 'information');
		}
		$style->addRenderRight($driver);
		$style->render();
		$done = true;
	}
}

if(!$done)
{
	include_once('includes/renderInclude.php');
	
	
	if(isset($_GET['p']))
	{
		$page = $_GET['p'];
		$start = ($page * 50) - 50;
	}
	else
	{
		$page = 1;
		$start = 0;
	}
		
	$q = 'SELECT COUNT(*) as row_count FROM ' . USERSGROUPS_TABLE . ' g,' . DRIVERNAME_TABLE . ' d 
		  WHERE d.user_id = g.user_id AND g.group_id = 3';
		  
	$res = $db->sql_query($q);
	if ( $row = $db->sql_fetchrow($res)  )
	{
		$row['row_count'] = ceil($row['row_count'] / 50);
		$text = '';
		for($x = 1; $x <= $row['row_count']; $x++)
		{
			if($page == $x)
			{
				$text .= $x . " | ";
			}
			else
			{
				$text .= "<a href='drivers.php?p=" . $x . "'>" . $x . "</a> | ";
			}
		}
		
		if($text != '')
		{
			$text = substr($text, 0 , -2);
		}
		$text = $lang['DriversText'] . '<br><br>' . $lang['Page'] . ': ' . $text;
		
	}
	else
	{
		$text = $lang['DriversText'];
	}
	
	
	// Create query
	$q = 'SELECT *, pf_drivername AS user_drivername,
				(select group_concat(t_name SEPARATOR ", ") 
					   from ' . TEAMMEMBERS_TABLE . ', ' . TEAMS_TABLE .
					 ' where tm_team = t_id and tm_approved = 1 and tm_driver = u.user_id) AS user_teams 
		  FROM ' . USERS_TABLE . ' u, '. USERSGROUPS_TABLE . ' g,' . DRIVERNAME_TABLE . ' d 
		  WHERE d.user_id = u.user_id AND g.user_id = u.user_id AND g.group_id = 3 
		  ORDER BY pf_drivername ASC 
		  LIMIT ' . $start . ', 50';

	$style = new style(false);				
	$style->addRenderLeft(new addMessage($text,$lang['Drivers']));
	$style->addRenderLeft(new renderInclude('tpl/tableStart.tpl'));
	// Run query
	$res = $db->sql_query($q);
	while ( $row = $db->sql_fetchrow($res))
	{
		$style->addRenderLeft(new driver_details($row, 'Mini'));
	}
	$style->addRenderLeft(new renderInclude('tpl/tableEnd.tpl'));
	
	$style->render();
}


echo $db->num_queries;
?>